﻿using System;
using System.Collections;
using System.Reflection;
using log4net;
using log4net.Config;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.ServiceModel;
using System.ComponentModel;
using System.ServiceModel.Security;
using System.Collections.Generic;
using System.ServiceModel.Description;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace HaiFang
{
    [Serializable]
    public class ProxyUserInfo
    {
        private static string PROCEDURE_CheckLogin = "CheckLogin";
        private static string PROCEDURE_QueryUserInfo = "QueryUserInfo";
        private static string PROCEDURE_InsertUserInfo = "InsertUserInfo";


        //@UserName			varchar(128),
        //@Password			varchar(128),
        //@LoginIp			varchar(17),
        //@usbKey				varchar(200)='',	--usbkey校验检查，为空表示不需要usbkey
        //@ErrorMSG			int	output			--100：用户名或口令不正确    300：该用户尝试登录过多，已被锁定  400:未使用自己授权优盘   0:登录成功 
        public static UserInfoWithProject CheckLogin(string UserName, string Password, string LoginIp, string usbKey, out int ErrorMSG)
        {
            UserInfoWithProject r = null;
            ErrorMSG = -1;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_CheckLogin;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@UserName", UserName)));
                    Parameters.Add(new SqlParameter("@Password", Password));
                    Parameters.Add(new SqlParameter("@LoginIp", LoginIp));
                    Parameters.Add(new SqlParameter("@usbKey", usbKey));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@ErrorMSG",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = ErrorMSG
                    });
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    r = helper.GetObject(typeof(UserInfoWithProject), dt) as UserInfoWithProject;
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@ErrorMSG")
                        {
                            ErrorMSG = helper.GetInt(sp.Value);
                            break;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                ErrorMSG = -1;
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return r;
        }

        //@CurrUserID			int = null,					--管理员编号
        //@Flag				int=1,						--1:查询全部数据  2：查询组用户数据  3：查询用户数据
        //@AutoID				bigint=0					--查询所有时传0，否则传编号值
        public static IList<UserInfoWithGroup> QueryUserInfo(int CurrUserID,int Flag,int AutoID)
        {
            IList<UserInfoWithGroup> list = new List<UserInfoWithGroup>();
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_QueryUserInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add((new SqlParameter("@Flag", Flag)));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    #endregion

                    #region 获取执行结果
                    DataTable dt = helper.ExecuteDataTable(comandText, CommandType.StoredProcedure, Parameters);
                    list = helper.GetObjects<UserInfoWithGroup>(dt);
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
            return list;
        }

        //@CurrUserID			int = null,					--管理员编号
        //@UseFlag			tinyint,					--1:insert  2:update  3:delete
        //@AutoID				bigint=0,					--@UseFlag为1时传0，其它操作传编号值
        //@GUID               varchar(128)='',			--GUID
        //@GroupID			bigint,
        //@UserName			varchar(20)='',		
        //@OldPassword		varchar(50)='',	
        //@Password			varchar(50)='',			
        //@Description		varchar(128)='',
        //@ErrorMSG			int	output					--100：用户名相同；200：旧口令不正确 0：正确；其他：错误	
        public static void InsertUserInfo(int CurrUserID, int UseFlag, int AutoID, string GUID, int GroupID, string UserName, string OldPassword, string Password, string Description, out int ErrorMSG)
        {
            ErrorMSG = -1;
            try
            {
                using (SqlHelper helper = new SqlHelper())
                {
                    #region 构造参数
                    string comandText = PROCEDURE_InsertUserInfo;
                    List<SqlParameter> Parameters = new List<SqlParameter>();
                    Parameters.Add((new SqlParameter("@CurrUserID", CurrUserID)));
                    Parameters.Add(new SqlParameter("@UseFlag", UseFlag));
                    Parameters.Add(new SqlParameter("@AutoID", AutoID));
                    Parameters.Add(new SqlParameter("@GUID", GUID));
                    Parameters.Add(new SqlParameter("@GroupID", GroupID));
                    Parameters.Add(new SqlParameter("@UserName", UserName));
                    Parameters.Add(new SqlParameter("@OldPassword", OldPassword));
                    Parameters.Add(new SqlParameter("@Password", Password));
                    Parameters.Add(new SqlParameter("@Description", Description));
                    Parameters.Add(new SqlParameter()
                    {
                        ParameterName = "@ErrorMSG",
                        SqlDbType = SqlDbType.Int,
                        Direction = ParameterDirection.Output,
                        Value = ErrorMSG
                    });
                    #endregion

                    #region 获取执行结果
                    helper.Execute(comandText, CommandType.StoredProcedure, Parameters);
                    #endregion

                    #region 获取output值
                    foreach (SqlParameter sp in Parameters)
                    {
                        if (sp.ParameterName == "@ErrorMSG")
                        {
                            ErrorMSG = helper.GetInt(sp.Value);
                            break;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                LogHelper.Writelog(LogType.LogType_Error, ex.Message, ex);
            }
        }
    }

    [Serializable]
    public class UserInfo
    {
        public int AutoID { get; set; }
        public string GUID { get; set; }
        public int GroupID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public DateTime CreateTime { get; set; }
        public string Description { get; set; }
        public DateTime LastLoginTime { get; set; }
        public DateTime LastCheckTime { get; set; }
        public int LockCount { get; set; }
        public DateTime LockTime { get; set; }
    }

    [Serializable]
    public class UserInfoWithProject : UserInfo
    {
        public int ProjectID { get; set; }
    }

    [Serializable]
    public class UserInfoWithGroup : UserInfo
    {
        public string GroupName { get; set; }
    }

    [Serializable]
    public class UserInfoWithPassword : UserInfo
    {
        public string OldPassword { get; set; }
    }
}
